namespace SilverSea.LPRProcessing
{
    using LPR_Wrapper;
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Threading;
    using System.Windows.Forms;

    public class IMPSLPRProcessing : ILPRProcessing
    {
        private bool bool_0;
        private int int_0 = 8;
        private int int_1 = 120;
        private Library library_0;
        private PlateFeatures plateFeatures_0 = PlateFeatures.Both;
        private PlateNumberFixing plateNumberFixing_0;
        private string string_0 = string.Empty;
        private string string_1 = "VN";
        private string string_2 = string.Empty;

        public event ErrorEventHandler ErrorEvent;

        public bool Analyze(string fileName, ref string[] plateNumber)
        {
            try
            {
                if (this.library_0 != null)
                {
                    LPR_Result result = null;
                    Bitmap bitmap = (Bitmap) Image.FromFile(fileName);
                    result = this.library_0.RecognizeLicensePlate(bitmap);
                    Application.DoEvents();
                    if (result != null)
                    {
                        plateNumber = new string[] { this.method_0(plateNumber[0]) };
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                this.CloseLPREngine();
                Thread.Sleep(0x3e8);
                this.CreateLPREngine();
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
            finally
            {
                GC.Collect();
            }
            return false;
        }

        public bool Analyze(string fileName, ref string plateNumber)
        {
            try
            {
                if (this.library_0 != null)
                {
                    LPR_Result result = null;
                    Bitmap bitmap = (Bitmap) Image.FromFile(fileName);
                    result = this.library_0.RecognizeLicensePlate(bitmap);
                    Application.DoEvents();
                    if (result != null)
                    {
                        plateNumber = result.LicensePlateNumber;//LicensePlateNumber;
                        plateNumber = this.method_0(plateNumber);
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                this.CloseLPREngine();
                Thread.Sleep(0x3e8);
                this.CreateLPREngine();
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
            finally
            {
                GC.Collect();
            }
            return false;
        }

        public bool Analyze(string fileName, ref string[] plateNumber, ref Bitmap[] plateImage)
        {
            try
            {
                if (this.library_0 != null)
                {
                    LPR_Result result = null;
                    Bitmap img = (Bitmap) Image.FromFile(fileName);
                    result = this.library_0.RecognizeLicensePlate(img);
                    Application.DoEvents();
                    if (result != null)
                    {
                        plateNumber = new string[1];
                        plateImage = new Bitmap[1];
                        plateNumber[0] = result.LicensePlateNumber;
                        plateNumber[0] = this.method_0(plateNumber[0]);
                        if (((result.LicensePlateLocation.X >= 0) && (result.LicensePlateLocation.Y >= 0)) && ((result.LicensePlateLocation.Width >= 0) && (result.LicensePlateLocation.Height >= 0)))
                        {
                            plateImage[0] = Untils.CropImage(img, result.LicensePlateLocation);
                        }
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                this.CloseLPREngine();
                Thread.Sleep(0x3e8);
                this.CreateLPREngine();
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
            finally
            {
                GC.Collect();
            }
            return false;
        }

        public bool Analyze(string fileName, ref string plateNumber, ref Bitmap plateImage)
        {
            try
            {
                if (this.library_0 != null)
                {
                    LPR_Result result = null;
                    Bitmap img = (Bitmap) Image.FromFile(fileName);
                    result = this.library_0.RecognizeLicensePlate(img);
                    Application.DoEvents();
                    if (result != null)
                    {
                        plateNumber = result.LicensePlateNumber;
                        plateNumber = this.method_0(plateNumber);
                        if (((result.LicensePlateLocation.X >= 0) && (result.LicensePlateLocation.Y >= 0)) && ((result.LicensePlateLocation.Width >= 0) && (result.LicensePlateLocation.Height >= 0)))
                        {
                            plateImage = Untils.CropImage(img, result.LicensePlateLocation);
                        }
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                this.CloseLPREngine();
                Thread.Sleep(0x3e8);
                this.CreateLPREngine();
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
            finally
            {
                GC.Collect();
            }
            return false;
        }

        public bool Analyze(string fileName, Bitmap vehicleImage, ref string plateNumber, ref int recognizeTime, ref Bitmap plateImage)
        {
            try
            {
                if (this.library_0 != null)
                {
                    LPR_Result result = null;
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    result = this.library_0.RecognizeLicensePlate(vehicleImage);
                    Application.DoEvents();
                    stopwatch.Stop();
                    if (result != null)
                    {
                        plateNumber = result.LicensePlateNumber;
                        plateNumber = this.method_0(plateNumber);
                        if (((result.LicensePlateLocation.X >= 0) && (result.LicensePlateLocation.Y >= 0)) && ((result.LicensePlateLocation.Width >= 0) && (result.LicensePlateLocation.Height >= 0)))
                        {
                            plateImage = Untils.CropImage(vehicleImage, result.LicensePlateLocation);
                        }
                        recognizeTime = (int) stopwatch.ElapsedMilliseconds;
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                this.CloseLPREngine();
                Thread.Sleep(0x3e8);
                this.CreateLPREngine();
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
            finally
            {
                GC.Collect();
            }
            return false;
        }

        public bool Analyze(string fileName, Bitmap vehicleImage, ref string[] plateNumber, ref int recognizeTime, ref Bitmap[] plateImage, ref float[] confidence, ref bool[] isBlackOnWhite)
        {
            try
            {
                if (this.library_0 != null)
                {
                    LPR_Result result = null;
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    result = this.library_0.RecognizeLicensePlate(vehicleImage);
                    Application.DoEvents();
                    stopwatch.Stop();
                    if (result != null)
                    {
                        plateNumber = new string[1];
                        plateImage = new Bitmap[1];
                        confidence = new float[1];
                        isBlackOnWhite = new bool[1];
                        plateNumber[0] = result.LicensePlateNumber;
                        plateNumber[0] = this.method_0(plateNumber[0]);
                        if (((result.LicensePlateLocation.X >= 0) && (result.LicensePlateLocation.Y >= 0)) && ((result.LicensePlateLocation.Width >= 0) && (result.LicensePlateLocation.Height >= 0)))
                        {
                            plateImage[0] = Untils.CropImage(vehicleImage, result.LicensePlateLocation);
                        }
                        recognizeTime = (int) stopwatch.ElapsedMilliseconds;
                        confidence[0] = result.ConfidenceLevel;//.get_ConfidenceLevel();
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                this.CloseLPREngine();
                Thread.Sleep(0x3e8);
                this.CreateLPREngine();
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
            finally
            {
                GC.Collect();
            }
            return false;
        }

        public bool Analyze(string fileName, Bitmap vehicleImage, ref string plateNumber, ref int recognizeTime, ref Bitmap plateImage, ref float confidence, ref bool isBlackOnWhite)
        {
            try
            {
                if (this.library_0 != null)
                {
                    LPR_Result result = null;
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    result = this.library_0.RecognizeLicensePlate(vehicleImage);
                    Application.DoEvents();
                    stopwatch.Stop();
                    if (result != null)
                    {
                        plateNumber = result.LicensePlateNumber;
                        plateNumber = this.method_0(plateNumber);
                        if (((result.LicensePlateLocation.X >= 0) && (result.LicensePlateLocation.Y >= 0)) && ((result.LicensePlateLocation.Width >= 0) && (result.LicensePlateLocation.Height >= 0)))
                        {
                            plateImage = Untils.CropImage(vehicleImage, result.LicensePlateLocation);
                        }
                        recognizeTime = (int) stopwatch.ElapsedMilliseconds;
                        confidence = result.ConfidenceLevel;//.get_ConfidenceLevel();
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                this.CloseLPREngine();
                Thread.Sleep(0x3e8);
                this.CreateLPREngine();
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
            finally
            {
                GC.Collect();
            }
            return false;
        }

        public bool Analyze(string fileName, Bitmap vehicleImage, ref string plateNumber, ref int recognizeTime, ref Bitmap plateImage, string overlayText, int overlayFontSize, Color overlayColor)
        {
            try
            {
                if (this.library_0 != null)
                {
                    LPR_Result result = null;
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    result = this.library_0.RecognizeLicensePlate(vehicleImage);
                    Application.DoEvents();
                    stopwatch.Stop();
                    if (result != null)
                    {
                        plateNumber = result.LicensePlateNumber;
                        plateNumber = this.method_0(plateNumber);
                        if (((result.LicensePlateLocation.X >= 0) && (result.LicensePlateLocation.Y >= 0)) && ((result.LicensePlateLocation.Width >= 0) && (result.LicensePlateLocation.Height >= 0)))
                        {
                            plateImage = Untils.CropImage(vehicleImage, result.LicensePlateLocation);
                        }
                        recognizeTime = (int) stopwatch.ElapsedMilliseconds;
                        if (overlayText != string.Empty)
                        {
                            Graphics graphics = Graphics.FromImage(vehicleImage);
                            Brush brush = new SolidBrush(overlayColor);
                            Font font = new Font("Microsoft Sans Serif", (float) overlayFontSize);
                            if ((result.LicensePlateLocation.Y < (vehicleImage.Width / 2)) && (result.LicensePlateLocation.Width != 0))
                            {
                                graphics.DrawString(DateTime.Now.ToString(), font, brush, new PointF(0f, (float) ((vehicleImage.Height - overlayFontSize) - 10)));
                            }
                            else
                            {
                                graphics.DrawString(DateTime.Now.ToString(), font, brush, new PointF(0f, 0f));
                            }
                            vehicleImage.Save(fileName, ImageFormat.Jpeg);
                            brush.Dispose();
                            graphics.Dispose();
                        }
                        return true;
                    }
                }
            }
            catch (Exception exception)
            {
                this.CloseLPREngine();
                Thread.Sleep(0x3e8);
                this.CreateLPREngine();
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
            finally
            {
                GC.Collect();
            }
            return false;
        }

        public void CloseLPREngine()
        {
            this.library_0 = null;
        }

        public void CreateLPREngine()
        {
            try
            {
                this.library_0 = new Library();
                this.plateNumberFixing_0 = new PlateNumberFixing();
                this.plateNumberFixing_0.CountryCode = this.string_1;
                this.plateNumberFixing_0.ProvinceCode = this.string_2;
                this.plateNumberFixing_0.PlateFeature = this.plateFeatures_0;
                this.plateNumberFixing_0.EnableMotorUnder50cc = this.bool_0;
            }
            catch (Exception exception)
            {
                if (this.ErrorEvent != null)
                {
                    this.ErrorEvent(this, new ErrorEventArgs(exception.ToString()));
                }
            }
        }

        private string method_0(string string_3)
        {
            string_3 = string_3.Trim(new char[] { '_' });
            if (string_3.Length > 0)
            {
                if (string_3.Length == 6)
                {
                    if (char.IsLetter(string_3[2]) && char.IsLetter(string_3[3]))
                    {
                        string_3 = string_3.Substring(0, 2) + "-" + string_3.Substring(2, 2) + "-" + string_3.Substring(4);
                    }
                    return string_3;
                }
                if ((string_3.Length <= 6) || (string_3.Length > 9))
                {
                    return string_3;
                }
                if (this.plateFeatures_0 != PlateFeatures.Both)
                {
                    if (char.IsLetter(string_3[2]) && char.IsLetter(string_3[3]))
                    {
                        string_3 = string_3.Substring(0, 4) + "-" + string_3.Substring(4);
                    }
                    else if (char.IsLetter(string_3[2]))
                    {
                        if (string_3.Length == 7)
                        {
                            string_3 = string_3.Substring(0, 3) + "-" + string_3.Substring(3);
                        }
                        else if (string_3.Length == 9)
                        {
                            string_3 = string_3.Substring(0, 2) + "-" + string_3.Substring(2, 2) + "-" + string_3.Substring(4);
                        }
                        else if (this.plateFeatures_0 == PlateFeatures.MotoOnly)
                        {
                            string_3 = string_3.Substring(0, 2) + "-" + string_3.Substring(2, 2) + "-" + string_3.Substring(4);
                        }
                        else if (this.plateFeatures_0 == PlateFeatures.CarOnly)
                        {
                            string_3 = string_3.Substring(0, 3) + "-" + string_3.Substring(3);
                        }
                    }
                    if (string_3.Contains("-"))
                    {
                        string_3 = this.plateNumberFixing_0.Fix(string_3);
                    }
                    return string_3;
                }
                char ch = string_3[2];
                if (ch.ToString() == "8")
                {
                    string_3 = string_3.Substring(0, 2) + "B" + string_3.Substring(3);
                    return string_3;
                }
                char ch2 = string_3[3];
                if (ch2.ToString() == "Z")
                {
                    string_3 = string_3.Remove(3, 1).Insert(3, "2");
                    return string_3;
                }
                char ch3 = string_3[3];
                if (ch3.ToString() == "S")
                {
                    string_3 = string_3.Remove(3, 1).Insert(3, "5");
                }
            }
            return string_3;
        }

        public string CountryCode
        {
            get
            {
                return this.string_1;
            }
            set
            {
                this.string_1 = value;
            }
        }

        public bool EnableMotorUnder50cc
        {
            get
            {
                return this.bool_0;
            }
            set
            {
                this.bool_0 = value;
            }
        }

        public string LPREngineProductKey
        {
            get
            {
                return this.string_0;
            }
            set
            {
                this.string_0 = value;
            }
        }

        public int MaxCharHeight
        {
            get
            {
                return this.int_1;
            }
            set
            {
                this.int_1 = value;
            }
        }

        public int MinCharHeight
        {
            get
            {
                return this.int_0;
            }
            set
            {
                this.int_0 = value;
            }
        }

        public PlateFeatures PlateFeature
        {
            get
            {
                return this.plateFeatures_0;
            }
            set
            {
                this.plateFeatures_0 = value;
            }
        }

        public string ProvinceCode
        {
            get
            {
                return this.string_2;
            }
            set
            {
                this.string_2 = value;
            }
        }
    }
}

